﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using CIRDiagnoseDevice.DBManage;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;
using DevExpress.XtraPrinting;


namespace CIRDiagnoseDevice
{
    public partial class DataQuery : Form
    {
        PrintRecord printRecord;
        TestRecordManage manage;
        DbManage dbMana = new DbManage();
        DataTable table = new DataTable();
        private bool isNoRecord = false; //标志查询到的记录条数是否为0 
        DeviceInfo device;
        List<QueryInfo> queryCondition = new List<QueryInfo>(); // 查询条件集
        List<QueryInfo> queryCondition2 = new List<QueryInfo>(); // 查询条件集2
        bool IsAddAnd;   //标记是否添加"CIR编号"相等判断查询
        struct QueryInfo
        {
            public string queryStr;
            public string queryName;
        }QueryInfo queryInfo;

        public DataQuery()
        {
            InitializeComponent();
            manage = new TestRecordManage();
            this.device = MainFrm.device;
        }

        private void printbn_Click(object sender, EventArgs e)
        {
            if (gridView1.RowCount > 0 || gridView4.RowCount>0)
            {

                //PrintingSystem ps = null;
                DevExpress.XtraPrinting.PrintableComponentLink link = null;

                printingSystem1 = new DevExpress.XtraPrinting.PrintingSystem();
                link = new DevExpress.XtraPrinting.PrintableComponentLink(printingSystem1);
                printingSystem1.Links.Add(link);
                link.Component = Gridctl_BriefRecord;
                  printableComponentLink1.Component  =Gridctl_BriefRecord ;
                printableComponentLink1.ShowPreview();
            }
            else
            {
                MessageBox.Show("没有要打印的数据");
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 加载已经测试过的设备的编号
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DataQuery_Load(object sender, EventArgs e)
        {
            dev_num.Properties.Items.Clear();
            DataTable dt = manage.QueryDevice();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dev_num.Properties.Items.Add(dt.Rows[i][0]);
            }
            testTime.DateTime = System.DateTime.Now;
            testTime2.DateTime = System.DateTime.Now;
            queryInfo.queryName = "test_rec.测试时间";
            queryInfo.queryStr = testTime.Text;
            queryCondition.Add(queryInfo);
            queryInfo.queryName = "测试时间2";
            queryInfo.queryStr = testTime2.Text;
            queryCondition.Add(queryInfo);
        }

        private string GetDetailSQLStr()
        {
            StringBuilder sql = new StringBuilder("Select * From test_rec,deviceInfo");
            return SQlCondition(sql).Append(" order by '测试时间'").ToString();

        }
        private string GetBriefSQLStr()
        {
            StringBuilder sql = new StringBuilder("Select case count(distinct [测试结果]) when 2 then '不合格' when 1 then '合格' else '数据异样,请查看详细结果' end as [测试结果], ");
            sql.Append("[测试时间],[测试员],[模块名称], 机车属局, 机车属段, 机车属所, 机车编号, 机车端别, 机车修程, CIR属局, CIR属段, CIR属车间, CIR属工区, CIR生产商, CIR型号, CIR编号, CIR修程 From test_rec,deviceInfo");
            StringBuilder sqlwhere = SQlCondition(sql);
            sqlwhere.Append(" and [测试结果]<>'__' group by [测试时间],[模块名称],[测试员], 机车属局, 机车属段, 机车属所, 机车编号, 机车端别, 机车修程, CIR属局, CIR属段, CIR属车间, CIR属工区, CIR生产商, CIR型号, CIR编号, CIR修程 ");
            return sqlwhere.ToString();
        }

        private StringBuilder SQlCondition(StringBuilder sql)
        {
            StringBuilder subsql = new StringBuilder();
            StringBuilder subsql2 = new StringBuilder();
            int isFirst = 0;
            if (queryCondition.Count > 0)
            {
                for (int i = 0; i < queryCondition.Count; i++)
                {
                    if (i == 0)
                    {
                        if (isFirst == 0)
                        {
                            if (queryCondition[i].queryName == "test_rec.测试时间" || queryCondition[i].queryName == "test_rec.测试时间2")
                            {
                                subsql.Append(" Where ");
                                QueryInfo endTime = queryCondition.Find(EndTime);
                                if (endTime.queryName != null)
                                {
                                    subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, endTime.queryStr));
                                }
                                else
                                {
                                    subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, queryCondition[i].queryStr));
                                }
                                isFirst++;
                            }
                            else if (queryCondition[i].queryName != "测试时间2")
                            {
                                subsql.Append(" Where ");
                                subsql.Append(string.Format("{0} like '%{1}%'", queryCondition[i].queryName, queryCondition[i].queryStr));
                                isFirst++;
                            }
                        }
                        else
                        {
                            if (queryCondition[i].queryName == "test_rec.测试时间")
                            {
                                subsql.Append(" And ");
                                QueryInfo endTime = queryCondition.Find(EndTime);
                                if (endTime.queryName != null)
                                {
                                    subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, endTime.queryStr));
                                }
                                else
                                {
                                    subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, queryCondition[i].queryStr));
                                }
                            }
                            else if (queryCondition[i].queryName != "测试时间2")
                            {
                                subsql.Append(" And ");
                                subsql.Append(string.Format("{0} like '%{1}%'", queryCondition[i].queryName, queryCondition[i].queryStr));
                            }
                        }
                    }
                    else
                    {
                        if (queryCondition[i].queryName == "test_rec.测试时间")
                        {
                            if (isFirst == 0)
                                subsql.Append(" Where ");
                            else
                                subsql.Append(" And ");
                            QueryInfo endTime = queryCondition.Find(EndTime);
                            if (endTime.queryName != null)
                            {
                                subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, endTime.queryStr));
                            }
                            else
                            {
                                subsql.Append(string.Format("{0} between '{1}' And '{2}'", queryCondition[i].queryName, queryCondition[i].queryStr, queryCondition[i].queryStr));
                            }
                        }
                        else if (queryCondition[i].queryName != "测试时间2")
                        {
                            subsql.Append(" And ");
                            subsql.Append(string.Format("{0} like '%{1}%'", queryCondition[i].queryName, queryCondition[i].queryStr));
                        }
                    }
                }
            }
            if (queryCondition2.Count > 0)
            {
                for (int i = 0; i < queryCondition2.Count; i++)
                {
                    if (i == 0)
                    {
                        if (isFirst == 0)
                        {
                            subsql2.Append(" Where ");
                            subsql2.Append(string.Format("{0} like '%{1}%'", queryCondition2[i].queryName, queryCondition2[i].queryStr));
                            isFirst++;
                        }
                        else
                        {
                            subsql2.Append(" And ");
                            subsql2.Append(string.Format("{0} like '%{1}%'", queryCondition2[i].queryName, queryCondition2[i].queryStr));
                        }
                    }
                    else
                    {
                        if (isFirst == 0)
                            subsql2.Append(" Where ");
                        else
                            subsql2.Append(" And ");
                        subsql2.Append(string.Format("{0} like '%{1}%'", queryCondition2[i].queryName, queryCondition2[i].queryStr));
                    }
                }
            }
            string unionStr = " And test_rec.设备编号 = deviceInfo.CIR编号";

            sql.Append(subsql.ToString());
            sql.Append(subsql2.ToString());
            if (subsql.ToString() == "" && subsql2.ToString() == "")
            {
                Gridctl_DetailRecord.DataSource = null;
                Gridctl_DetailRecord.Invalidate();
                isNoRecord = true;
                return sql;
            }
            sql.Append(unionStr);
            return sql;
        }

        private bool EndTime(QueryInfo que)
        {
            if (que.queryName == "测试时间2")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        private void searchB_Click(object sender, EventArgs e)
        {

            string sql;
            if (isNoRecord)
            {
                MessageBox.Show("查询条件不能为空");
                isNoRecord = false;
                return;
            }
            try
            {
                if (chb_BriefSearch.Checked)
                {
                    sql = GetBriefSQLStr();
                    DataTable dt = manage.QueryData(sql);
                    if (dt.Rows.Count == 0 || dt == null)
                    {
                        isNoRecord = true;
                        Gridctl_DetailRecord.DataSource = null;
                        Gridctl_DetailRecord.Invalidate();
                        return;

                    }
                    SetDeviceINF(dt,3);
                    Gridctl_BriefRecord.DataSource = dt;
                    Gridctl_BriefRecord.Invalidate();
                    Gridctl_DetailRecord.Visible = false;
                    Gridctl_BriefRecord.Visible = true; 
                }
                else
                {
                    sql = GetDetailSQLStr();
                    DataTable dt = manage.QueryData(sql);
                    if (dt.Rows.Count == 0 || dt == null)
                    {
                        isNoRecord = true;
                        Gridctl_DetailRecord.DataSource = null;
                        Gridctl_DetailRecord.Invalidate();

                    }
                    else
                    {
                        SetDeviceINF(dt,9);
                       
                        dt.Columns.RemoveAt(0); 
                        Gridctl_DetailRecord.DataSource = dt;
                        Gridctl_DetailRecord.Invalidate();
                        Gridctl_DetailRecord.Visible = true;
                        Gridctl_BriefRecord.Visible = false;
                        SetXraGridViewColWordrap(gridView1, "测试项目");
                        SetXraGridViewColWordrap(gridView1, "测试值");

                    } 
                }
            }
            catch
            {

            }

            // MessageBox.Show(sql);

        }
        /// <summary>
        /// 设置设备的信息以供打印时候填写表头
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="j"></param>
        private void SetDeviceINF(DataTable dt,int j)
        {

            cab_bur.Text = dt.Rows[0][j].ToString();
            cab_seg.Text = dt.Rows[0][j+1].ToString();
            cab_pl.Text = dt.Rows[0][j + 2].ToString();
            cab_num.Text = dt.Rows[0][j + 3].ToString();
            cab_db.Text = dt.Rows[0][j + 4].ToString();
            cab_xc.Text = dt.Rows[0][j +5].ToString();
            dev_bur.Text = dt.Rows[0][j + 6].ToString();
            dev_seg.Text = dt.Rows[0][j + 7].ToString();
            dev_ws.Text = dt.Rows[0][j + 8].ToString();
            dev_wa.Text = dt.Rows[0][j + 9].ToString();
            dev_man.Text = dt.Rows[0][j + 10].ToString();
            dev_mod.Text = dt.Rows[0][j + 11].ToString();
            dev_num.Text = dt.Rows[0][j + 12].ToString();
            dev_xc.Text = dt.Rows[0][j + 13].ToString();
            device.Cab_bur = cab_bur.Text.Trim();
            device.Cab_db = Convert.ToInt32(cab_db.Text.Trim());
            device.Cab_num = cab_num.Text.Trim();
            device.Cab_pl = cab_pl.Text.Trim();
            device.Cab_seg = cab_seg.Text.Trim();
            device.Cab_xc = cab_xc.Text.Trim();
            device.Dev_bur = dev_bur.Text.Trim();
            device.Dev_man = dev_man.Text.Trim();
            device.Dev_mod = dev_mod.Text.Trim();
            device.Dev_num = dev_num.Text.Trim();
            device.Dev_seg = dev_seg.Text.Trim();
            device.Dev_wa = dev_wa.Text.Trim();
            device.Dev_ws = dev_ws.Text.Trim();
            device.Dev_xc = dev_xc.Text.Trim(); 
            testTime.DateTime = (DateTime)dt.Rows[0][1];
            dt.Columns.RemoveAt(1);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (tester.Properties.Items.Contains(dt.Rows[i][j-2].ToString()) == false)
                {
                    tester.Properties.Items.Add(dt.Rows[i][j-2].ToString());
                }
            }
            tester.Text = dt.Rows[0][j-2].ToString();
        }
        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (isNoRecord)
            {
                if (this.gridView1.RowCount == 0)
                {
                    string str = "没有查询到该设备的相关测试记录";
                    System.Drawing.Font font = new System.Drawing.Font("宋体", 10.5f, FontStyle.Bold);
                    System.Drawing.Rectangle r = new System.Drawing.Rectangle(e.Bounds.Left + 100, e.Bounds.Top + 100,
                                                                  e.Bounds.Width - 100, e.Bounds.Height - 100);
                    e.Graphics.DrawString(str, font, Brushes.SteelBlue, r);

                }
            }
        }

        /// <summary>    
        /// 设置某列自动换行
        /// </summary>    
        /// <param name="p_XtraGridView">待设置的XtraGridView</param>    
        /// <param name="p_FieldName">列名</param>    
        public static void SetXraGridViewColWordrap(DevExpress.XtraGrid.Views.Grid.GridView p_XtraGridView, string p_FieldName)
        {
            //设置允许自动换行所需的一些属性       
            DevExpress.XtraGrid.Columns.GridColumn col = p_XtraGridView.Columns[p_FieldName];
            if (!p_XtraGridView.OptionsView.RowAutoHeight)
                p_XtraGridView.OptionsView.RowAutoHeight = true;
            if (!col.AppearanceCell.Options.UseTextOptions)
                col.AppearanceCell.Options.UseTextOptions = true;
            if (col.AppearanceCell.TextOptions.WordWrap != DevExpress.Utils.WordWrap.Wrap)
                col.AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;

            //用RepositoryItemMemoEdit控件作为显示控件,以便换行
            DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit res = null;
            DevExpress.XtraEditors.Repository.RepositoryItem resOld = col.ColumnEdit;
            if (resOld != null)
            {
                if (resOld is DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit)
                    res = resOld as DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit;
                else
                {
                    p_XtraGridView.GridControl.RepositoryItems.Remove(resOld);
                    resOld.Dispose();
                }
            }

            if (res == null)
            {
                res = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
                p_XtraGridView.GridControl.RepositoryItems.Add(res);
                col.ColumnEdit = res;
            }
        }

        private void dev_num_Leave(object sender, EventArgs e)
        {
            try
            {
                if (sender is DevExpress.XtraEditors.TextEdit)
                {
                    DevExpress.XtraEditors.TextEdit obj = sender as DevExpress.XtraEditors.TextEdit;

                    queryInfo.queryStr = obj.Text.Trim();
                    if (obj.Name == "dev_num")
                    {
                        queryInfo.queryName = "test_rec.设备编号";
                        AddItem(queryInfo, queryCondition);
                    }
                    else if (obj.Name == "dev_bur")
                    {
                        queryInfo.queryName = "CIR属局";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_seg")
                    {
                        queryInfo.queryName = "CIR属段";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_ws")
                    {
                        queryInfo.queryName = "CIR属车间";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_wa")
                    {
                        queryInfo.queryName = "CIR属工区";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_mod")
                    {
                        queryInfo.queryName = "CIR型号";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_xc")
                    {
                        queryInfo.queryName = "CIR修程";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "dev_man")
                    {
                        queryInfo.queryName = "CIR生产商";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_db")
                    {
                        queryInfo.queryName = "机车端别";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_xc")
                    {
                        queryInfo.queryName = "机车修程";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_bur")
                    {
                        queryInfo.queryName = "机车属局";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_seg")
                    {
                        queryInfo.queryName = "机车属段";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_pl")
                    {
                        queryInfo.queryName = "机车属所";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "cab_num")
                    {
                        queryInfo.queryName = "机车编号";
                        AddItem(queryInfo, queryCondition2);
                    }
                    else if (obj.Name == "tester")
                    {
                        queryInfo.queryName = "test_rec.测试员";
                        AddItem(queryInfo, queryCondition);
                    }
                    else if (obj.Name == "testTime")
                    {
                        queryInfo.queryName = "test_rec.测试时间";
                        AddItem(queryInfo, queryCondition);
                    }
                    else if (obj.Name == "testTime2")
                    {
                        queryInfo.queryName = "测试时间2";
                        AddItem(queryInfo, queryCondition);
                    }
                }
            }
            catch
            {
            }
        }

        //添加查询条件
        private void AddItem(QueryInfo queryInfo, List<QueryInfo> list)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].queryName == queryInfo.queryName)
                {
                    if (queryInfo.queryStr != "")
                    {
                        list.RemoveAt(i);
                        list.Insert(i, queryInfo);
                    }
                    else
                        list.RemoveAt(i);
                }
            }
            if (list.Contains(queryInfo))
                return;
            else
            {
                if (queryInfo.queryStr != "")
                    list.Add(queryInfo);
            }
        }

        private void clearSearch_B_Click(object sender, EventArgs e)
        {
            cab_bur.Text = "";
            cab_seg.Text = "";
            cab_pl.Text = "";
            cab_num.Text = "";
            cab_db.Text = "";
            cab_xc.Text = "";
            dev_bur.Text = "";
            dev_seg.Text = "";
            dev_ws.Text = "";
            dev_wa.Text = "";
            dev_man.Text = "";
            dev_mod.Text = "";
            dev_num.Text = "";
            dev_xc.Text = "";
            tester.Text = "";
            testTime.EditValue = "";
            testTime2.EditValue = "";
            queryCondition.Clear();
            queryCondition2.Clear();
        }

        private void testTime_EditValueChanged(object sender, EventArgs e)
        {
            queryInfo.queryName = "test_rec.测试时间";
            queryInfo.queryStr = testTime.Text;
            for (int i = 0; i < queryCondition.Count; i++)
            {
                if (queryCondition[i].queryName == queryInfo.queryName)
                {
                    queryCondition.RemoveAt(i);
                    queryCondition.Insert(i, queryInfo);
                }
            }
        }

        private void exportbn_Click(object sender, EventArgs e)
        {
            string fileName = string.Empty;
            if (chb_BriefSearch.Checked)
            {
                if (gridView4.RowCount > 0)
                {
                    saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = ExportPDF(caption, gridView4, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("没有要导出的数据！");
                }
            }
            else
            {
                if (gridView1.RowCount > 0)
                {
                    saveFileDialog1.FileName = device.Dev_num + " 设备测试记录";
                    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
                    {
                        string filePath = saveFileDialog1.FileName;
                        string caption = device.Dev_num + "测试记录";
                        bool result = ExportPDF(caption, gridView1, filePath);
                        if (!result)
                        {
                            MessageBox.Show("文件导出失败！");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("没有要导出的数据！");
                }
            }
        }

        private void SetFooter(PdfWriter writer)
        {
            PdfReportM1HeaderFooter headerFooter = new PdfReportM1HeaderFooter();
            writer.SetBoxSize("art", PageSize.A4);
            writer.PageEvent = headerFooter;
        }

        private bool ExportPDF(string strCaption, GridView dgv, string fileName)
        {
            try
            {
                FileStream file = new FileStream(fileName, FileMode.Create);
                iTextSharp.text.Document document = new iTextSharp.text.Document(PageSize.A4);//创建一个Document实例
                document.NewPage();
                PdfWriter writer = PdfWriter.GetInstance(document, file);//创建Writer实例       
                this.SetFooter(writer);
                document.Open();
                BaseFont baseFont = CreateChineseFont();

                iTextSharp.text.Font titleFont = new iTextSharp.text.Font(baseFont, 20, iTextSharp.text.Font.BOLD);
                iTextSharp.text.Font fontUnderLine = new iTextSharp.text.Font(baseFont, 12, iTextSharp.text.Font.UNDERLINE);
                iTextSharp.text.Font normalFont = new iTextSharp.text.Font(baseFont, 12);
                iTextSharp.text.Font normalRedFont = new iTextSharp.text.Font(baseFont, 12, iTextSharp.text.Font.NORMAL | iTextSharp.text.Font.BOLD, BaseColor.RED);
                float titleLineHeight = 45f, normalLineHeight = 25f;
                Paragraph pBlank = new Paragraph(" ", normalFont);
                pBlank.Leading = normalLineHeight;
                Paragraph titleP = new Paragraph(strCaption, titleFont);
                titleP.Leading = titleLineHeight; titleP.Alignment = Element.ALIGN_CENTER;
                document.Add(titleP);
                document.Add(pBlank);
                document.Add(manage.CreateTable(dgv, device, tester.Text));
                document.Close();
                return true;
            }
            catch
            {
                //  MessageBox.Show("报表导出失败！");
                return false;
            }
        }


        /// <summary>
        /// 创建中文字体(实现中文)
        /// </summary>
        /// <returns></returns>
        public static BaseFont CreateChineseFont()
        {
            BaseFont.AddToResourceSearch("iTextAsian.dll");
            BaseFont.AddToResourceSearch("iTextAsianCmaps.dll"); //"STSong-Light", "UniGB-UCS2-H", 
            BaseFont baseFT = BaseFont.CreateFont(@"c:\windows\fonts\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

            //iTextSharp.text.Font font = new iTextSharp.text.Font(baseFT);
            return baseFT;
        }

        private void printableComponentLink1_CreateReportHeaderArea(object sender, DevExpress.XtraPrinting.CreateAreaEventArgs e)
        {
            StringBuilder headerStr = new StringBuilder();
            string str = string.Empty;
            str = String.Format(dev_num.Text.Trim() + "设备测试记录\n\n\n");
            headerStr.Append(str);
            str = String.Format("测试时间： " + testTime.Text.Trim() + "         测试员： " + tester.Text.Trim());
            string str1 = String.Format("机车配属：" + device.Cab_bur + "铁路局    " + device.Cab_seg + "段     " + device.Cab_pl + "所");
            string str2 = String.Format("机车端别：" + device.Cab_db + "     机车编号：" + device.Cab_num + "    机车修程：" + device.Cab_xc);
            string str3 = String.Format("CIR设备配属：" + device.Dev_bur + "铁路局    " + device.Dev_seg + "段   " + device.Dev_ws + "车间    " + device.Dev_wa + "工区");
            string str4 = String.Format("CIR设备型号：" + device.Dev_mod + "   CIR设备编号：" + device.Dev_num + "    CIR设备生产商：" + device.Dev_man + "   CIR设备修程：" + device.Dev_xc);

            e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Center);
            e.Graph.Font = new System.Drawing.Font("宋体", 14, FontStyle.Bold);
            RectangleF rec = new RectangleF(0, 0, e.Graph.ClientPageSize.Width, 50);
            e.Graph.DrawString(headerStr.ToString(), Color.Black, rec, BorderSide.None);

            e.Graph.StringFormat = new BrickStringFormat(StringAlignment.Near);
            e.Graph.Font = new System.Drawing.Font("宋体", 12, FontStyle.Bold);

            rec = new RectangleF(0, 50, e.Graph.ClientPageSize.Width, 25);
            e.Graph.DrawString(str, Color.Black, rec, BorderSide.None);

            rec = new RectangleF(0, 75, e.Graph.ClientPageSize.Width, 25);
            e.Graph.DrawString(str1, Color.Black, rec, BorderSide.None);

            rec = new RectangleF(0, 100, e.Graph.ClientPageSize.Width, 25);
            e.Graph.DrawString(str2, Color.Black, rec, BorderSide.None);

            rec = new RectangleF(0, 125, e.Graph.ClientPageSize.Width, 45);
            e.Graph.DrawString(str3, Color.Black, rec, BorderSide.None);

            rec = new RectangleF(0, 170, e.Graph.ClientPageSize.Width, 45);
            e.Graph.DrawString(str4, Color.Black, rec, BorderSide.None);

            PointF pf1 = new PointF(0, 45);
            PointF pf2 = new PointF(e.Graph.ClientPageSize.Width, 45);
            e.Graph.DrawLine(pf1, pf2, Color.Azure, 0.2f);
            pf1 = new PointF(0, 45);
            pf2 = new PointF(0, 215);
            e.Graph.DrawLine(pf1, pf2, Color.Azure, 0.2f);
            float x = e.Graph.ClientPageSize.Width - 1.0f;
            pf1 = new PointF(x, 45);
            pf2 = new PointF(x, 215);
            e.Graph.DrawLine(pf1, pf2, Color.Azure, 0.2f);
        }

        private void xtraTabPage1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
